package com.gtw.modules.api.controller;

import java.util.*;

import com.gtw.common.annotation.AuthIgnoreApi;
import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.PayStatus;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.dto.TbGroupBuyDto;
import com.gtw.dto.TbGroupBuyUserDto;
import com.gtw.dto.TbGroupCommodityDto;
import com.gtw.modules.api.service.*;
import com.gtw.modules.entity.*;
import com.gtw.utils.DateUtil;
import com.gtw.utils.OrderUtil;
import com.gtw.utils.UidUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 团购
 *
 * @author wuye
 * @email www.baidu.com
 * @date 2018-02-08 17:34:06
 */
@RestController
@RequestMapping("/api/groupbuy")
public class TbGroupbuyController {
    @Autowired
    private TbGroupbuyService groupbuyService;

    @Autowired
    private TbCommodityService tbCommodityService;

    @Autowired
    private TbGroupbuyUserService tbGroupbuyUserService;

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TbOrderService tbOrderService;

    @Autowired
    private TbAddressService tbAddressService;

    @Autowired
    private TbShoppingCartServices tbShoppingCartServices;

    /**
     * 查询团购商品列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<TbGroupbuy> groupbuyList = groupbuyService.queryList(query);
        int total = groupbuyService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(groupbuyList, total, query.getLimit(), query.getPage());

        return R.ok().put("data", pageUtil);
    }


    /**
     * 查询指定团购商品列表
     *
     * @return
     */
    @AuthIgnoreApi
    @RequestMapping("/foundByGroupbuyid")
    public R foundByGroupbuyid(@RequestParam Integer groupbuyid) {
        TbGroupbuy tbGroupbuy = groupbuyService.queryObject(groupbuyid);
        TbCommodity tbCommodity = tbCommodityService.queryObject(tbGroupbuy.getCommodityid());
        TbGroupCommodityDto dto = new TbGroupCommodityDto();
        dto.setTbCommodity(tbCommodity);
        dto.setReserve(tbGroupbuy.getReserve());
        dto.setPrice(tbGroupbuy.getCurrentprice());
        dto.setEndtime(tbGroupbuy.getEndtime());
        return R.ok().put("data", dto);
    }


    /**
     * 获取全平台正在进行中的团购
     * isstart = 1 and isopen = 1
     *
     * @return
     */
    @AuthIgnoreApi
    @GetMapping("/foundStart")
    public R foundStart(@RequestParam Map<String, Object> params) {
        params.put("identity", 0);
        Query query = new Query(params);
        //查询所有开团
        List<TbGroupbuyUser> groupbuyUserList = tbGroupbuyUserService.queryByUserId(params);
        int total = tbGroupbuyUserService.queryTotal(params);
        List<TbGroupBuyDto> dtoList = new ArrayList<>();
        for (TbGroupbuyUser tgbu : groupbuyUserList) {
            TbGroupbuy tbGroupbuy = groupbuyService.queryObject(tgbu.getGroupbuyid());
            TbCommodity tbCommodity = tbCommodityService.queryObject(tbGroupbuy.getCommodityid());
            TbGroupBuyDto dto = new TbGroupBuyDto();
            dto.setId(tgbu.getId());
            dto.setTbCommodity(tbCommodity);
            dto.setPrice(tgbu.getPrice());
            dto.setIdentity(tgbu.getIdentity());
            dto.setNum(tgbu.getPepnum());
            dto.setOpentime(tgbu.getCreatetime());
            dto.setEndtime(DateUtil.DateTime(tgbu.getCreatetime()));
            TbUser tbUser = tbUserService.queryById(tgbu.getUserid());
            tbUser.setPaypwd("");
            tbUser.setPassword("");
            dto.setUser(tbUser);
            dtoList.add(dto);
        }
        PageUtils pageUtil = new PageUtils(dtoList, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }


    /**
     * 查询可发起开团商品
     * isstart = 0 and isopen = 0
     *
     * @return
     */
    @AuthIgnoreApi
    @GetMapping("/foundOpen")
    public R foundOpen(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<TbGroupbuy> groupbuyList = groupbuyService.queryList(params);
        int total = groupbuyService.queryTotal(query);
        List<TbGroupBuyDto> dtoList = new ArrayList<>();
        for (TbGroupbuy tgp : groupbuyList) {
            List<TbGroupbuyUser> groupbuyUserList = tbGroupbuyUserService.queryByGroupId(tgp.getId());
            TbCommodity tbCommodity = tbCommodityService.queryObject(tgp.getCommodityid());
            TbGroupBuyDto dto = new TbGroupBuyDto();
            dto.setTbCommodity(tbCommodity);
            dto.setNum(groupbuyUserList.size());
            dto.setId(tgp.getId());
            dto.setOpentime(tgp.getStarttime());
            dto.setPrice(tgp.getCurrentprice());
            dto.setReserve(tgp.getReserve());
            dto.setEndtime(DateUtil.DateTime(dto.getOpentime()));
            dtoList.add(dto);
        }
        PageUtils pageUtil = new PageUtils(dtoList, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }

    /**
     * 发起团购
     * 限时24小时
     * 团长原价开团
     * 4人以下
     * 团成员均价
     * 4人至6人
     * 团长半价，团员均价
     * 7人满团
     * 团长免费，团员均价
     *
     * @return
     */
    @RequestMapping("/openGroupBuy")
    public R openGroupBuy(@RequestParam Integer groupbuyid, @RequestParam Integer commodityid, @LoginUserApi TbUser tbUser) {
        //判断该用户是否已经发起过这个团购,判断该用户是否为这个团购的团长
        Map<String, Object> map = new HashedMap();
        map.put("leaderid", tbUser.getUserId());
        map.put("groupid", groupbuyid);
        List<TbGroupbuyUser> tgbu = tbGroupbuyUserService.queryByTeam(map);
        if (tgbu.size() > 0) {
            return R.error().put("msg", "已经发起过团购了");
        } else {
            TbGroupbuy tbGroupbuy = groupbuyService.queryObject(groupbuyid);
            //判断团购库存是否充足
            if (tbGroupbuy.getReserve() > 7) {
                //发起团购
                List<TbGroupbuyUser> groupbuyUserList1 = tbGroupbuyUserService.queryByTeam(map);
                TbGroupbuyUser tbGroupbuyUser = new TbGroupbuyUser();
                tbGroupbuyUser.setLeaderid(tbUser.getUserId());
                tbGroupbuyUser.setPepnum(groupbuyUserList1.size() + 1);
                tbGroupbuyUser.setIdentity(0);
                tbGroupbuyUser.setPrice(tbGroupbuy.getCurrentprice());
                tbGroupbuyUser.setUserid(tbUser.getUserId());
                tbGroupbuyUser.setGroupbuyid(groupbuyid);
                tbGroupbuyUser.setCreatetime(new Date());
                tbGroupbuyUser.setEndtime(DateUtil.DateTime(tbGroupbuyUser.getCreatetime()));
                tbGroupbuyUser.setIsend(0);
                tbGroupbuyUserService.insert(tbGroupbuyUser);
                tbGroupbuy.setReserve(tbGroupbuy.getReserve() - 1);
                groupbuyService.update(tbGroupbuy);
                return R.ok().put("endtime", tbGroupbuyUser.getEndtime()).put("data", tbGroupbuyUser);
            } else {
                return R.error().put("msg", "库存不足以开团");
            }
        }
    }

    /**
     * 参与团购
     * 所有人按人数均价
     *
     * @return
     */
    @RequestMapping("/joinGroupBuy")
    public R joinGroupBuy(@LoginUserApi TbUser tbUser, @RequestParam Integer groupbuyuserid) {

        TbGroupbuyUser tggbu = tbGroupbuyUserService.selectByPrimaryKey(groupbuyuserid);
        Map<String, Object> hashmap = new HashMap<>();
        hashmap.put("leaderid", tggbu.getLeaderid());
        hashmap.put("groupid", tggbu.getGroupbuyid());
        //判断不能重复参与团购
        List<TbGroupbuyUser> list = tbGroupbuyUserService.queryByTeam(hashmap);
        for (TbGroupbuyUser tgbu : list) {
            if (tbUser.getUserId().equals(tgbu.getUserid())) {
                return R.error().put("msg", "已经参与过此次团购");
            }
        }

        //参与团购
        TbGroupbuyUser tgbu = tbGroupbuyUserService.selectByPrimaryKey(groupbuyuserid);
        Map<String, Object> map = new HashMap<>();
        map.put("leaderid", tgbu.getLeaderid());
        map.put("groupid", tgbu.getGroupbuyid());
        List<TbGroupbuyUser> groupbuyUserList2 = tbGroupbuyUserService.queryByTeam(map);
        TbGroupbuy tbGroupbuy = groupbuyService.queryObject(tgbu.getGroupbuyid());
        TbGroupbuyUser tbGroupbuyUser = new TbGroupbuyUser();

        //获取商品原价
        Float olemoney = tbGroupbuy.getCurrentprice();
        //计算均价
        Float avgmoney = (olemoney / (groupbuyUserList2.size() + 1));
        tgbu.setPrice(avgmoney);

        tbGroupbuyUser.setLeaderid(tgbu.getUserid());
        tbGroupbuyUser.setIsend(0);
        tbGroupbuyUser.setCreatetime(new Date());
        tbGroupbuyUser.setPrice(avgmoney);
        tbGroupbuyUser.setEndtime(tgbu.getEndtime());
        tbGroupbuyUser.setIdentity(1);
        tbGroupbuyUser.setGroupbuyid(tgbu.getGroupbuyid());
        tbGroupbuyUser.setPepnum(groupbuyUserList2.size() + 1);
        tbGroupbuyUser.setUserid(tbUser.getUserId());
        tbGroupbuyUserService.insert(tbGroupbuyUser);
        tbGroupbuy.setReserve(tbGroupbuy.getReserve() - 1);
        groupbuyService.update(tbGroupbuy);
        List<TbGroupbuyUser> groupbuyUserList3 = tbGroupbuyUserService.queryByTeam(map);
        for (TbGroupbuyUser gbu : groupbuyUserList3) {
            gbu.setPrice(avgmoney);
            tbGroupbuyUserService.updateByPrimaryKey(gbu);
        }
        if (groupbuyUserList3.size() >= 7) {
            //结束团购，生成订单
            endGroupBuy(tbGroupbuyUser);
        }
        return R.ok().put("endtime", tbGroupbuyUser.getEndtime()).put("data", tbGroupbuyUser);


        //4人以下 团成员均价 团长原价
//        if (groupbuyUserList2.size() < 4) {
//            //获取团长
//            Float moeny = tbGroupbuy.getCurrentprice();
//            //4人一下均价
//            Float avgmoney =moeny/(groupbuyUserList2.size()+1);
//            TbGroupbuyUser tbGroupbuyUser = new TbGroupbuyUser();
//            tbGroupbuyUser.setLeaderid(tgbu.getUserid());
//            tbGroupbuyUser.setIsend(0);
//            tbGroupbuyUser.setCreatetime(new Date());
//            tbGroupbuyUser.setPrice(avgmoney);
//            tbGroupbuyUser.setEndtime(tgbu.getEndtime());
//            tbGroupbuyUser.setIdentity(1);
//            tbGroupbuyUser.setGroupbuyid(tgbu.getGroupbuyid());
//            tbGroupbuyUser.setPepnum(groupbuyUserList2.size() + 1);
//            tbGroupbuyUser.setUserid(tbUser.getUserId());
//            tbGroupbuyUserService.insert(tbGroupbuyUser);
//            tbGroupbuy.setReserve(tbGroupbuy.getReserve() - 1);
//            groupbuyService.update(tbGroupbuy);
//            return R.ok().put("endtime", tbGroupbuyUser.getEndtime()).put("data", tbGroupbuyUser);
//        }
//        //4-6人  团员均价   团长半价
//        else if (groupbuyUserList2.size() >= 4 && groupbuyUserList2.size() <6) {
//            //获取团长
//            tgbu.setPrice(tgbu.getPrice() / 2);
//            Float moeny = tbGroupbuy.getCurrentprice();
//            Float avgmoney = (moeny / (groupbuyUserList2.size()+1));
//            TbGroupbuyUser tbGroupbuyUser = new TbGroupbuyUser();
//            tbGroupbuyUser.setLeaderid(tgbu.getUserid());
//            tbGroupbuyUser.setIsend(0);
//            tbGroupbuyUser.setCreatetime(new Date());
//            tbGroupbuyUser.setPrice(avgmoney);
//            tbGroupbuyUser.setEndtime(tgbu.getEndtime());
//            tbGroupbuyUser.setIdentity(1);
//            tbGroupbuyUser.setGroupbuyid(tgbu.getGroupbuyid());
//            tbGroupbuyUser.setPepnum(groupbuyUserList2.size() + 1);
//            tbGroupbuyUser.setUserid(tbUser.getUserId());
//            tbGroupbuyUserService.insert(tbGroupbuyUser);
//            tbGroupbuyUserService.updateByPrimaryKey(tgbu);
//            tbGroupbuy.setReserve(tbGroupbuy.getReserve() - 1);
//            groupbuyService.update(tbGroupbuy);
//            return R.ok().put("endtime", tbGroupbuyUser.getEndtime()).put("data", tbGroupbuyUser);
//        }
//
//        //7人满团   团员均价  团长免费
//        else{
//            //获取团长
//            tgbu.setPrice(0f);
//            Float moeny = tbGroupbuy.getCurrentprice();
//            Float avgmoney = (moeny /(groupbuyUserList2.size()+1));
//            TbGroupbuyUser tbGroupbuyUser = new TbGroupbuyUser();
//            tbGroupbuyUser.setLeaderid(tgbu.getUserid());
//            tbGroupbuyUser.setIsend(0);
//            tbGroupbuyUser.setCreatetime(new Date());
//            tbGroupbuyUser.setPrice(avgmoney);
//            tbGroupbuyUser.setEndtime(tgbu.getEndtime());
//            tbGroupbuyUser.setIdentity(1);
//            tbGroupbuyUser.setGroupbuyid(tgbu.getGroupbuyid());
//            tbGroupbuyUser.setPepnum(groupbuyUserList2.size() + 1);
//            tbGroupbuyUser.setUserid(tbUser.getUserId());
//            tbGroupbuyUserService.insert(tbGroupbuyUser);
//            tbGroupbuyUserService.updateByPrimaryKey(tgbu);
//            tbGroupbuy.setReserve(tbGroupbuy.getReserve() - 1);
//            groupbuyService.update(tbGroupbuy);
//            //当第七人参加之后 立即结束团购
//            endGroupBuy(tbGroupbuyUser);
//            return R.ok().put("endtime", tbGroupbuyUser.getEndtime()).put("data", tbGroupbuyUser);
//        }
    }


    public void endGroupBuy(TbGroupbuyUser tbGroupbuyUser) {
        Map<String, Object> map = new HashedMap();
        map.put("leaderid", tbGroupbuyUser.getLeaderid());
        map.put("groupid", tbGroupbuyUser.getGroupbuyid());
        List<TbGroupbuyUser> groupbuyUserList = tbGroupbuyUserService.queryByTeam(map);
        //生成订单
        for (TbGroupbuyUser tgu : groupbuyUserList) {
            //团长订单
            if (tgu.getLeaderid().equals(tgu.getUserid())) {
                TbUser tbUser = tbUserService.queryById(tgu.getUserid());
                //查询商品信息
                TbGroupbuy tbGroupbuy = groupbuyService.queryObject(tgu.getGroupbuyid());
                TbCommodity tbCommodity = tbCommodityService.queryObject(tbGroupbuy.getCommodityid());
                //创建购物车数据
                TbShoppingCart tbShoppingCart = new TbShoppingCart();
                tbShoppingCart.setStatus(true);
                tbShoppingCart.setCommoditynumber(1);
                tbShoppingCart.setPricediscounts(tbCommodity.getPricediscounts());
                tbShoppingCart.setPrice(tgu.getPrice());
                tbShoppingCart.setFreight(0f);
                tbShoppingCart.setCommodityname(tbCommodity.getCommodityname());
                tbShoppingCart.setCommoditypic(tbCommodity.getPicurl());
                tbShoppingCart.setShoppingtime(new Date());
                tbShoppingCart.setSelected(null);
                tbShoppingCart.setUid(tbUser.getUuid());
                tbShoppingCart.setCommodityid(tbCommodity.getId());
                tbShoppingCartServices.insert(tbShoppingCart);
                tgu.setIsend(1);
                tbGroupbuyUserService.updateByPrimaryKey(tgu);
                TbOrder tbOrder = new TbOrder();
                tbOrder.setUid(tbUser.getUuid());
                tbOrder.setChannel("1");
                tbOrder.setIsdelete(false);
                tbOrder.setShoppingcartid("[" + String.valueOf(tbShoppingCart.getId()) + "]");
                tbOrder.setIsfirst(1);
                tbOrder.setUsername(tbUser.getUsername());
                tbOrder.setTotalfee(tgu.getPrice());
                tbOrder.setPaytype(PayStatus.unpaid.getName());
                tbOrder.setBillno(OrderUtil.getOrderNo());
                tbOrder.setGuid(UidUtil.getUUID());
                tbOrder.setIspay(false);
                tbOrder.setCommodityid(tbGroupbuy.getCommodityid());
                tbOrder.setRemark("团长");
                tbOrder.setAddressid(null);
                tbOrder.setCreatedate(new Date());
                tbOrder.setOrderstatus(PayStatus.unpaid.getName());
                tbOrder.setIsproxy(0);
                List<TbAddress> addressList = tbAddressService.findbyUid(tbUser.getUuid());
                for (TbAddress adre : addressList) {
                    if (adre.getIsdefault().equals("1")) {
                        tbOrder.setAddressid(adre.getId());
                    }
                }
                tbOrderService.save(tbOrder);
            } else {
                TbUser tbUser = tbUserService.queryById(tgu.getUserid());
                //查询商品信息
                TbGroupbuy tbGroupbuy = groupbuyService.queryObject(tgu.getGroupbuyid());
                TbCommodity tbCommodity = tbCommodityService.queryObject(tbGroupbuy.getCommodityid());
                //创建购物车数据
                TbShoppingCart tbShoppingCart = new TbShoppingCart();
                tbShoppingCart.setStatus(true);
                tbShoppingCart.setCommoditynumber(1);
                tbShoppingCart.setPricediscounts(tbCommodity.getPricediscounts());
                tbShoppingCart.setPrice(tgu.getPrice());
                tbShoppingCart.setFreight(0f);
                tbShoppingCart.setCommodityname(tbCommodity.getCommodityname());
                tbShoppingCart.setCommoditypic(tbCommodity.getPicurl());
                tbShoppingCart.setShoppingtime(new Date());
                tbShoppingCart.setSelected(null);
                tbShoppingCart.setUid(tbUser.getUuid());
                tbShoppingCart.setCommodityid(tbCommodity.getId());
                tbShoppingCartServices.insert(tbShoppingCart);
                tgu.setIsend(1);
                tbGroupbuyUserService.updateByPrimaryKey(tgu);
                TbOrder tbOrder = new TbOrder();
                tbOrder.setUid(tbUser.getUuid());
                tbOrder.setChannel("1");
                tbOrder.setIsdelete(false);
                tbOrder.setShoppingcartid("[" + String.valueOf(tbShoppingCart.getId()) + "]");
                tbOrder.setIsfirst(1);
                tbOrder.setUsername(tbUser.getUsername());
                tbOrder.setTotalfee(tgu.getPrice());
                tbOrder.setPaytype(PayStatus.unpaid.getName());
                tbOrder.setBillno(OrderUtil.getOrderNo());
                tbOrder.setGuid(UidUtil.getUUID());
                tbOrder.setIspay(false);
                tbOrder.setCommodityid(tbGroupbuy.getCommodityid());
                tbOrder.setRemark("团员");
                tbOrder.setAddressid(null);
                tbOrder.setCreatedate(new Date());
                tbOrder.setOrderstatus(PayStatus.unpaid.getName());
                tbOrder.setIsproxy(0);
                List<TbAddress> tbAddressList = tbAddressService.findbyUid(tbUser.getUuid());
                for (int k = 0; k < tbAddressList.size(); k++) {
                    if (tbAddressList.size() > 0) {
                        if (tbAddressList.get(k).getIsdefault()) {
                            tbOrder.setAddressid(tbAddressList.get(k).getId());
                            break;
                        } else {
                            tbOrder.setAddressid(tbAddressList.get(k).getId());
                        }
                    }
                }
                List<TbAddress> addressList = tbAddressService.findbyUid(tbUser.getUuid());
                for (TbAddress adre : addressList) {
                    if (adre.getIsdefault().equals("1")) {
                        tbOrder.setAddressid(adre.getId());
                    }
                }
                tbOrderService.save(tbOrder);
            }


//                    tbGroupbuyUser.setIsend(1);
//                    tbGroupbuyUserService.updateByPrimaryKey(tbGroupbuyUser);
//                    TbUser tbUser = tbUserService.queryById(tbGroupbuyUser.getUserid());
//                    TbOrder tbOrder = new TbOrder();
//                    tbOrder.setUid(tbUser.getUuid());
//                    tbOrder.setChannel("1");
//                    tbOrder.setIsdelete(false);
//                    tbOrder.setIsfirst(1);
//                    tbOrder.setUsername(tbUser.getUsername());
//                    tbOrder.setTotalfee(tbGroupbuyUser.getPrice());
//                    tbOrder.setPaytype(PayStatus.unpaid.getName());
//                    tbOrder.setBillno(OrderUtil.getOrderNo());
//                    tbOrder.setGuid(UidUtil.getUUID());
//                    tbOrder.setIspay(false);
//                    TbGroupbuy tbGroupbuy = groupbuyService.queryObject(tbGroupbuyUser.getGroupbuyid());
//                    tbOrder.setCommodityid(tbGroupbuy.getCommodityid());
//                    tbOrder.setRemark("团购");
//                    tbOrder.setAddressid(null);
//                    tbOrder.setCreatedate(new Date());
//                    tbOrder.setOrderstatus(PayStatus.unpaid.getName());
//                    tbOrder.setIsproxy(0);
//                    List<TbAddress> addressList = tbAddressService.findbyUid(tbUser.getUuid());
//                    for (TbAddress adre:addressList) {
//                        if(adre.getIsdefault().equals("1")){
//                            tbOrder.setAddressid(adre.getId());
//                        }
//                    }

        }
    }

    /**
     * 查询用户参与和发起的团购
     *
     * @return
     */
    @RequestMapping("/fundByuser")
    public R fundByuser(@RequestParam Map<String, Object> params, @LoginUserApi TbUser tbUser) {
        params.put("userid", tbUser.getUserId());
        Query query = new Query(params);
        List<TbGroupbuyUser> groupbuyUserList = tbGroupbuyUserService.queryByUserId(query);
        int total = tbGroupbuyUserService.queryTotal(query);
        List<TbGroupBuyDto> dtoList = new ArrayList<>();
        for (TbGroupbuyUser tgbu : groupbuyUserList) {
            TbGroupbuy tbGroupbuy = groupbuyService.queryObject(tgbu.getGroupbuyid());
            TbCommodity tbCommodity = tbCommodityService.queryObject(tbGroupbuy.getCommodityid());
            TbGroupBuyDto dto = new TbGroupBuyDto();
            dto.setId(tgbu.getId());
            dto.setTbCommodity(tbCommodity);
            dto.setStatus(tbGroupbuy.getIsstart());
            dto.setIdentity(tgbu.getIdentity());
            dto.setPrice(tgbu.getPrice());
            Map<String, Object> map = new HashMap<>();
            map.put("leaderid", tgbu.getLeaderid());
            map.put("groupid", tgbu.getGroupbuyid());
            dto.setNum(tbGroupbuyUserService.queryByTeam(map).size());
            dto.setOpentime(tgbu.getCreatetime());
            dto.setEndtime(DateUtil.DateTime(dto.getOpentime()));
            dto.setIsend(tgbu.getIsend());
            TbUser user = tbUserService.queryById(tgbu.getLeaderid());
            dto.setUser(user);
            dtoList.add(dto);
        }

        PageUtils pageUtil = new PageUtils(dtoList, total, query.getLimit(), query.getPage());

        return R.ok().put("data", pageUtil);
    }

    /**
     * 查询用户参与和发起的团购的详细信息
     *
     * @return
     */
    @RequestMapping("/fundByuserByid")
    public R fundByuserByid(@RequestParam Integer groupuserid, @LoginUserApi TbUser tbUser) {
        //获取团购信息
        TbGroupbuyUser tbGroupbuyUser = tbGroupbuyUserService.selectByPrimaryKey(groupuserid);
        Map<String, Object> map = new HashedMap();
        map.put("leaderid", tbGroupbuyUser.getLeaderid());
        map.put("groupid", tbGroupbuyUser.getGroupbuyid());
        List<TbGroupbuyUser> tbGroupbuyUserList = tbGroupbuyUserService.queryByTeam(map);
        TbGroupBuyUserDto dto = new TbGroupBuyUserDto();
        List<TbUser> userList = new ArrayList<>();
        for (TbGroupbuyUser tgbu : tbGroupbuyUserList) {
            if (tgbu.getUserid().equals(tbUser.getUserId())) {
                dto.setMoney(tgbu.getPrice());
            }
            if (tgbu.getIdentity() == 0) {
                //团长
                TbUser tb = tbUserService.queryById(tgbu.getUserid());
                tb.setPassword("");
                tb.setPaypwd("");
                dto.setLeaderuser(tb);
            } else {
                //成员列表
                TbUser user = tbUserService.queryById(tgbu.getUserid());
                user.setPaypwd("");
                user.setPassword("");
                userList.add(user);
            }
        }
        dto.setTeamuser(userList);
        dto.setIsend(tbGroupbuyUser.getIsend());
        dto.setCreatetime(tbGroupbuyUser.getCreatetime());
        dto.setEndtime(tbGroupbuyUser.getEndtime());
        TbGroupbuy tbGroupbuy = groupbuyService.queryObject(tbGroupbuyUser.getGroupbuyid());

        TbCommodity tbCommodity = tbCommodityService.queryObject(tbGroupbuy.getCommodityid());

        dto.setCommodityidimage(tbCommodity.getPicurl());
        dto.setTbCommodityid(tbCommodity.getId());
        dto.setCommodityname(tbCommodity.getCommodityname());
        dto.setTbGroupbuy(tbGroupbuy);

        return R.ok().put("data", dto);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TbGroupbuy groupbuy = groupbuyService.queryObject(id);

        return R.ok().put("groupbuy", groupbuy);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TbGroupbuy groupbuy) {
        groupbuyService.save(groupbuy);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TbGroupbuy groupbuy) {
        groupbuyService.update(groupbuy);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        groupbuyService.deleteBatch(ids);

        return R.ok();
    }


}
